<template>
  <div class="category">
    <van-search shape="round" placeholder="请输入搜索关键词" disabled @click="openPopup" />
    <van-tree-select :items="categoryList" :main-active-index.sync="active" @click-nav="leftFn">
      <template #content>
        <!-- <van-image v-if="active === 0" src="https://img.yzcdn.cn/vant/apple-1.jpg" />
        <van-image v-if="active === 1" src="https://img.yzcdn.cn/vant/apple-2.jpg" />-->
        <div class="right">
          <div class="img">
            <img :src="currentCategory.banner_url" alt />
          </div>
          <p>{{currentCategory.front_name}}</p>
          <div class="mytitle">
            <span></span>
            <h4>{{currentCategory.name}}分类</h4>
          </div>
          <van-grid :column-num="3">
            <van-grid-item
              v-for="item in currentCategory.subCategoryList"
              @click="todetail(item.id)"
              :key="item.id"
              :icon="item.wap_banner_url"
              :text="item.name"
            />
          </van-grid>
        </div>
      </template>
    </van-tree-select>

    <transition name="slide">
      <keep-alive>
        <router-view></router-view>
      </keep-alive>
    </transition>
    <transition name="fade">
      <div class="popuoBg" v-if="$store.state.showPopup"></div>
    </transition>
  </div>
</template>
 
<script>
import { Catalog, CatalogCurrent } from "../request/api";

export default {
  data() {
    return {
      active: 0,
      items: [{ text: "分组 1" }, { text: "分组 2" }],
      categoryList: [],
      currentCategory: [],
    };
  },
  methods: {
    openPopup() {
      this.$router.push("/category/popup");
      this.$store.commit("changeShowPopup", true);
    },
    todetail(id) {
      this.$router.push("/channel/" + id);
    },
    getData() {
      Catalog().then((res) => {
        // console.log(res);
        this.categoryList = JSON.parse(
          JSON.stringify(res.data.categoryList).replace(/name/g, "text")
        );
        this.currentCategory = res.data.currentCategory;
      });
    },
    leftFn(i) {
      // console.log(this.categoryList[i].id);
      this.getdataCurrent(this.categoryList[i].id);
    },
    getdataCurrent(id) {
      CatalogCurrent({ id }).then((res) => {
        this.currentCategory = res.data.currentCategory;
      });
    },
  },
  created() {
    this.getData();
  },
};
</script>
 
<style lang = "less" scoped>
.right {
  text-align: center;
  position: relative;
  .img {
    position: relative;
    width: 96%;
    border-radius: 5px;
    box-shadow: 0 0 8px #000;
    overflow: hidden;
    z-index: 99;
    margin: auto;
    img {
      width: 100%;
      height: 140px;
      display: block;
    }
    &::after {
      content: "";
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.3);
      position: absolute;
      top: 0;
      left: 0;
    }
  }
  & > p {
    color: #fff;
    position: absolute;
    top: 40px;
    left: 50%;
    transform: translateX(-50%);
  }
  .mytitle {
    position: relative;
    height: 50px;
    line-height: 50px;
    span{
      width: 50%;
      height: 2px;
      background: #ccc;
      position: absolute;
      top: 24px;
      left: 50%;
      transform: translateX(-50%);
    }
    h4 {
      position: absolute;
      height: 50px;
      width: 30%;
      position: absolute;
      top: 0;
      left: 50%;
      transform: translateX(-50%);
      font-size: 16px;
      z-index: 10;
      background: #fff;

    }
  }
}

.van-tree-select__nav {
  flex: none;
}
.slide-enter,
.slide-leave-to {
  right: -100%;
}
.slide-enter-active,
.slide-leave-active {
  transition: right 0.5s;
}
.slide-enter-to,
.slide-leave {
  right: 0;
}

.fade-enter,
.fade-leave-to {
  opacity: 0.5;
}
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s;
}
.fade-enter-to,
.fade-leave {
  opacity: 1;
}
.popuoBg {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  overflow: scroll;
  z-index: 100;
}
.van-tree-select {
  background: #f7f8fa;
  height: calc(100vh - 104px) !important;
}
</style>